package cn.onlineclass.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class StringUtil {

    private StringUtil() {
    }

    private static SimpleDateFormat DF = new SimpleDateFormat("yyyy-MM-dd");

    public static int toInt(String s){
        return Integer.parseInt(s);
    }
    //转换字符串到数字，失败，返回默认值def
    public static int toInt(String s,int def){
        try {
            return Integer.parseInt(s);
        }catch (Exception e){
        }
        return def;
    }
    public static long toLong(String s){
        return Long.parseLong(s);
    }
    public static long toLong(String s,long def){
        try {
            return Long.parseLong(s);
        }catch (Exception e){
        }
        return def;
    }
    public static boolean isBlank(Object s) {
        return s == null || s.equals("");
    }

    public static String trim(String s) {
        return s==null?"":s.trim();
    }

    // 把str转换成日期格式，格式yyyy-MM-dd
    public static Date parse(String str) {
        try {
            synchronized (DF) {
                return DF.parse(str);
            }
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            // e.printStackTrace();
        }
        return null;
    }

    public static String format(Date d) {
        if (d == null)
            return "";
        synchronized (DF) {
            return DF.format(d);
        }
    }
}